package com.fwtai.web;

import com.fwtai.bean.PageFormData;
import com.fwtai.datasource.DaoHandle;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 区域商品库存访问数据库
 * @作者 李宇鹏
 * @版本 v1.0
 * @创建时间 2023-04-18 09:19:30
 * @QQ号码 444141300
 * @Email service@yinlz.com
 * @官网 <url>http://www.fwtai.com</url>
*/
@Repository
public class GoodsAreaDao{

  private final DaoHandle dao;

  public GoodsAreaDao(final DaoHandle dao){
    this.dao = dao;
  }

  @Transactional
  public int edit(final PageFormData formData,final HashMap<String,Object> map){
    final int rows1 = dao.execute("goods_area.edit",formData);
    final int rows2 = dao.execute("goods_area.add_record",map);
    return (rows1 > 0 && rows2 > 0) ? rows1 : 0;
  }

  public Long queryExistById(final long kid){
    return dao.queryForLong("goods_area.queryExistById",kid);
  }

  public HashMap<String,Object> queryById(final long kid){
    return dao.queryForHashMap("goods_area.queryById",kid);
  }

  public int delById(final long kid){
    return dao.execute("goods_area.delById",kid);
  }

  public int delByKeys(final long[] list){
    return dao.execute("goods_area.delByKeys",list);
  }

  public List<HashMap<String,Object>> getListData(final PageFormData formData){
    return dao.queryForListHashMap("goods_area.listData",formData);
  }

  public Integer getListTotal(final PageFormData formData){
    return dao.queryForInteger("goods_area.listTotal",formData);
  }

  public List<HashMap<String,Object>> getRecord(final long kid){
    return dao.queryForListHashMap("goods_area.getRecord",kid);
  }

  //获取表goods_area的字段goods_id的下拉框列动态值选项的数据
  public List<HashMap<String,Object>> getGoods_id(){
    return dao.queryForListHashMap("goods_area.getGoods_id");
  }

  //获取表goods_area的字段user_id的下拉框列动态值选项的数据
  public List<HashMap<String,Object>> getUser_id(){
    return dao.queryForListHashMap("goods_area.getUser_id");
  }

  public HashMap<String,Object> getAreaUserId(final String user_name){
    return dao.queryForHashMap("goods_area.getAreaUserId",user_name);
  }

  public HashMap<String,Object> getAreaUserId(final long userId){
    return dao.queryForHashMap("goods_area.getAreaUserInfo",userId);
  }

  public List<HashMap<String,Object>> getGoodsCoupon(){
    return dao.queryForListHashMap("goods_area.get_goods_coupon");
  }

  /**查询指定的商品id及商品id优惠券(由商品id及商品优惠券构成多种商品)*/
  public List<HashMap<String,Object>> getGoodsIdsCoupon(final Long[] goodsIds){
    return dao.queryForListHashMap("goods_area.getGoodsIdsCoupon",goodsIds);
  }

  public int addGoodsArea(final ArrayList<HashMap<String,Object>> allGoods){
    return dao.execute("goods_area.add_goods_area",allGoods);
  }

  public HashMap<String,Object> getGoodAreaInventory(final long kid){
    return dao.queryForHashMap("goods_area.getGoodAreaInventory",kid);
  }

  public int addLogRecord(final HashMap<String,Object> mapRecord){
    return dao.execute("goods_area.add_record",mapRecord);
  }

  public List<HashMap<String,Object>> getAreaCoupon(final HashMap<String,Object> params){
    return dao.queryForListHashMap("goods_area.getAreaCoupon",params);
  }

  public List<HashMap<String,Object>> getGoodsRecord(final long userId){
    return dao.queryForListHashMap("goods_area.getGoodsRecord",userId);
  }

  public List<HashMap<String,Object>> getStoreCoupon(final long userId){
    return dao.queryForListHashMap("goods_area.getStoreCoupon",userId);
  }

  public Integer getAreaLevel(final long areaId){
    return dao.queryForInteger("goods_area.getAreaLevel",areaId);
  }

  /**当level=1时,那areaId就是本身,仅需在service处理即可*/
  public HashMap<String,Object> getAreaCustom(final int level,final long areaId){
    switch (level){
      case 2:
        return dao.queryForHashMap("goods_area.proxy_city",areaId);
      case 3:
        return dao.queryForHashMap("goods_area.proxy_county",areaId);
      case 4:
        return dao.queryForHashMap("goods_area.proxy_towns",areaId);
      default://仅支持到街道级别
        return null;
    }
  }

  public List<HashMap<String,Object>> getOrderCoupon(final long outTradeNo){
    return dao.queryForListHashMap("goods_area.getOrderCoupon",outTradeNo);
  }

  public List<Long> getStoreGoodsCoupon(final HashMap<String,Object> mapCoupon){
    return dao.queryListEntity("goods_area.getStoreGoodsCoupon",mapCoupon);
  }
}